package cuShapes;

import java.awt.Color;
import javax.xml.bind.annotation.*;
import java.awt.Point;
import java.awt.Rectangle;
import palette.*;
//import xml.*;
//import javax.xml.bind.annotation.adapters.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="xmlellipse")
public class Ellipse extends Connectable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7816006054839289579L;
	@XmlTransient
	public static int nameCount = 0;

	/**************************************************************
	 * Implicit value constructor that sets all PQShape variables
	 * 
	 * creates an ellipse with the default size 5x5
	 * 
	 **************************************************************/
	// no-arg constructor needed by jaxb
	public Ellipse() {}
	
	public Ellipse(Point centerPoint, Color c){
		this(new Point(centerPoint.x, centerPoint.y),
				new Point(centerPoint.x+5, centerPoint.y+5), c);
	}
	
	public Ellipse(int x, int y, int width, int height){
		this(new Point(0, 0), new Point(0, 0), CurrentColor.getInstance());
	}
	
	public Ellipse(Point start, Point end, Color c) {
		super();
		color = c;
		name = "Ellipse #" + Integer.toString(nameCount++);
		lines.add(new ArcLine(start, end, 180, c));
		lines.add(new ArcLine(start, end, -180, c));
				
		width = end.x-start.x;
		height = end.y-start.y;
		
		//Set the center and resize
		center = new Rectangle(start.x+(width/2)-5,start.y+(height/2)-5,10,10);
		resize = new Rectangle(end.x, end.y, 10, 10);
		rect = new Rectangle(start.x,start.y,width, height);
	}
	
	public void setEnd(Point p) {
		super.setEnd(p);
    	lines.get(0).setEnd(p);
    	lines.get(1).setEnd(p);
    	resize.setLocation(p);
	}

	public void setStart(Point p) {
		super.setStart(p);
		lines.get(0).setStart(p);
		lines.get(1).setStart(p);
	}
	
	public String getShapeClass() {
		return "Ellipse";
	}
	
	public int getWidth() {
		return Math.abs(width);
	}
	
	public int getHeight() {
		return Math.abs(height);
	}
	
	public static void resetNames() {
		nameCount = 0;
	}
	
	private void updateDimensions() {
		width = Math.abs(lines.get(1).getStart().x - lines.get(0).getEnd().x);
		height = Math.abs(lines.get(1).getStart().y - lines.get(0).getEnd().y);
	}
	
	public void moveWorkingRect(Point p){
		   //moving.
		if(workingRect == center){
			//base everything off the center bc thats where the mouse click was
			center.setLocation(p.x-5,p.y-5); 
			
			// added mylesc: by switching the next line from setEnd to setStart and the 
			// following line from setStart to setEnd, when moved an Ellipse will no longer
			// flip the resize rect from bottom right to top left of shape. This is needed
			// because the PropertiesTableListener is looking for the resize rect to be in 
			// the bottom right.
			setStart(new Point(p.x-width/2, p.y-height/2));
			setEnd(new Point(p.x+width/2, p.y+height/2));
			rect.setLocation(p.x-width/2, p.y-height/2);
		}
		else if(workingRect == rect){
			
		}
		//resizing
		else{
			int xx = (p.x > lines.get(0).getStart().x) ? p.x : lines.get(0).getStart().x;
			int yy = (p.y > lines.get(0).getStart().y) ? p.y : lines.get(0).getStart().y;
			setEnd(p);
			updateDimensions();
			center.setLocation(xx-width/2-5, yy-height/2-5);
			//rect.setBounds(lines.get(0).getStart().x,lines.get(0).getStart().y, width, height);
			int ix = (p.x < lines.get(0).getStart().x) ? p.x : lines.get(0).getStart().x;
			int iy = (p.y < lines.get(0).getStart().y) ? p.y : lines.get(0).getStart().y;
			
			rect.setBounds(ix+2,iy+2, width-4, height-4);
		}
	}

	@Override
	public boolean hitRect(Point p) {
		boolean ret = false;
		if(center.contains(p)){
			workingRect = center;
			ret = true;
		}
		else if(resize.contains(p)){
			workingRect = resize;
			ret =  true;
		}
		return ret;
	}
	


}
